/**
 * 
 * 功能说明:角色管理
 * 
 * @author ZHONGFUHUA
 * 
 * @Date 2016年10月25日
 * 
 */
var sysRoleApp = angular.module('sysRoleApp', ["ngSanitize","ui.select", "commonApp", "sysServiceApp", "ngDropTree", "ngJsTree" ]);

sysRoleApp.controller('sysRoleCtrl', [
       "$scope",
       "commonService", 
       "sysService",
       "$timeout",
       "$log", 
       function($scope, commonService, sysService, $timeout, $log){
	

			$scope.queryUserParams = {
				orgId : null
			};

			$scope.queryPrivilegeParams = {
				privCatalogId : null
			};

			$scope.deleteUserParams = {
				systemRoleId : null,
				userIdsList : []
			}

			$scope.deletePrivParams = {
				systemRoleId : null,
				privilegeIdsList : []
			}

			$scope.deleteRoleParam = {
				id : null
			}
			
			$scope.roleId = null;
			
			$scope.action = "ADD_ROLE";

			$scope.submitRole = {
				userIdsList : [],
				privilegeIdsList : [],
				roleName : null,
				id : null
			};
			
			// 表格对象
			$scope.$userRoleSelectTable = $('#userRoleSelectTable');
			$scope.$rolePrivSelectTable = $('#rolePrivSelectTable');
			$scope.$userRoleTable = $('#userRoleTable');
			$scope.$rolePrivTable = $('#rolePrivTable');
			
			// 按钮对象
			$scope.$addRoleButton = $('#addRoleButton');
			$scope.$removeRoleButton = $('#removeRoleButton');
			$scope.$addUserButton = $('#addUserButton');
			$scope.$removeUserButton = $('#removeUserButton');
			$scope.$addPrivButton = $('#addPrivButton');
			$scope.$removePrivButton = $('#removePrivButton');
	
			// 表格选中实例对象
			$scope.rolePrivSelections = [];
			$scope.rolePrivSelectionIds = [];
			$scope.userRoleSelections = [];
			$scope.userRoleSelectionIds = [];
			$scope.rolePrivSelSelections = [];
			$scope.rolePrivSelSelectionIds = [];
			$scope.userRoleSelSelections = [];
			$scope.userRoleSelSelectionIds = [];
			$scope.selectedPrivileges = [];
			$scope.selectedUsers = [];
			
			// 模态框对象
			$scope.$roleEditModal = $('#roleEditModal');
		
			//监视isSucce的状态
			$scope.isSuccess = true;
			$scope.error = null;
			
			ffc.util.watchResult($scope, $timeout);	
	
	
		    /**
		     * 
		     * 功能说明:监视action的状态
		     * 
		     * @author ZHONGFUHUA
		     *
		     * @Date 2016年7月28日
		     *
		     */
			$scope.$watch('action',function(){
				if($scope.action == "ADD_ROLE"){
					$scope.showname=true;
					$scope.showuser=true;
					$scope.showprivilege=true;
				}
				else if($scope.action == "ADD_USER"){
					$scope.showname=false;
					$scope.showuser=true;
					$scope.showprivilege=false;
				}
				else{
					$scope.showname=false;
					$scope.showuser=false;
					$scope.showprivilege=true;
				}
			});
			
			/**
			 * 
			 * 功能说明:权限目录树配置
			 * 
			 * @author ZHONGFUHUA
			 * 
			 * @Date 2016年10月25日
			 * 
			 */
			$scope.roleTreeConfig = {
					config : {
					},
					types : {
						'default' : {
							icon : "glyphicon glyphicon-folder-open"
						},
						'catalog' : {
							icon : "glyphicon glyphicon-file"
						}
					},
					events : {
						selectNode : function(e, data) {
							$scope.$removeRoleButton.prop('disabled', false);
							$scope.roleId=data.selected[0];
							$scope.submitRole.id=data.selected[0];
							$scope.deleteUserParams.systemRoleId=data.selected[0];
							$scope.deletePrivParams.systemRoleId=data.selected[0];
							$scope.$userRoleTable.bootstrapTable("selectPage", 1);
							$scope.$rolePrivTable.bootstrapTable("selectPage", 1);
						}
					},
					icons : true,
					multiple : false,
					animation : 200,
					status : '',
					search : "",
					selected : [],
					plugins : [],
					data : []
			}
			
			

			/**
			 * 
			 * 功能说明:用户目录树配置
			 * 
			 * @author ZHONGFUHUA
			 * 
			 * @Date 2016年10月25日
			 * 
			 */
			$scope.orgTreeConfig = {
					config : {
					},
					types : {
						'default' : {
							icon : "glyphicon glyphicon-folder-open"
						},
						'catalog' : {
							icon : "glyphicon glyphicon-file"
						}
					},
					events : {
						selectNode : function(e, data) {
							$scope.queryUserParams.orgId=data.selected[0];
							$scope.$userRoleSelectTable.bootstrapTable("selectPage", 1);
						}
					},
					icons : true,
					multiple : false,
					animation : 200,
					status : '',
					search : "",
					selected : [],
					plugins : [],
					data : []
			}
			
			
			/**
			 * 
			 * 功能说明:权限目录树配置
			 * 
			 * @author ZHONGFUHUA
			 * 
			 * @Date 2016年10月25日
			 * 
			 */
			$scope.privTreeConfig = {
					config : {
					},
					types : {
						'default' : {
							icon : "glyphicon glyphicon-folder-open"
						},
						'catalog' : {
							icon : "glyphicon glyphicon-file"
						}
					},
					events : {
						selectNode : function(e, data) {
							$scope.queryPrivilegeParams.privCatalogId = data.selected[0];
							$scope.$rolePrivSelectTable.bootstrapTable("selectPage", 1);
						}
					},
					icons : true,
					multiple : false,
					animation : 200,
					status : '',
					search : "",
					selected : [],
					plugins : [],
					data : []
			}
			
			
			 
			/**
			 * 
			 * 功能说明:权限目录树初始化
			 * 
			 * @author ZHONGFUHUA
			 * 
			 * @Date 2016年10月25日
			 * 
			 */
			 $scope.initPrivTree=function(){
				 sysService.queryrolePrivCatalogTree({"children": []}, function(data) {
					 if (data.result) {
						 $scope.privTreeConfig.data = data.dataList;
					 }
				 });
			 }

			
	
			/**
			 * 
			 * 功能说明:角色目录树初始化
			 * 
			 * @author ZHONGFUHUA
			 * 
			 * @Date 2016年10月25日
			 * 
			 */
			$scope.initRoleTree=function(){
			   sysService.queryRoleTree({"children": []}, function(data) {
		           if (data.result) {
		        	   $scope.roleTreeConfig.data = data.dataList;
		           }
		       });
			};
	  
			
			/**
			 * 
			 * 功能说明:用户目录树初始化
			 * 
			 * @author ZHONGFUHUA
			 * 
			 * @Date 2016年10月25日
			 * 
			 */
			$scope.initOrgTree=function(){
			   sysService.queryOrgRoleTree({"children": []}, function(data) {
				   if (data.result) {
					   $scope.orgTreeConfig.data = data.dataList;
				   }
			   });
			}
	           

			/**
			 * 
			 * 功能说明:每次选择时重新获取表格中被选中对象
			 * 
			 * @author ZHONGFUHUA
			 * 
			 * @Date 2016年10月25日
			 * 
			 */
			$scope.$userRoleTable.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table', function () {
					$scope.$removeUserButton.prop('disabled', !$scope.$userRoleTable.bootstrapTable('getSelections').length);
					$scope.getUserRoleSelections();
			});
			
			/**
			 * 
			 * 功能说明:每次选择时重新获取表格中被选中对象
			 * 
			 * @author ZHONGFUHUA
			 * 
			 * @Date 2016年10月25日
			 * 
			 */
			$scope.$rolePrivTable.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table', function () {
					$scope.$removePrivButton.prop('disabled', !$scope.$rolePrivTable.bootstrapTable('getSelections').length);
					$scope.getRolePrivSelections();
			});
			
			
			/**
			 * 
			 * 功能说明:每次选择时重新获取表格中被选中对象
			 * 
			 * @author ZHONGFUHUA
			 * 
			 * @Date 2016年10月25日
			 * 
			 */
			$scope.$userRoleSelectTable.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table', function () {
					$scope.getUserRoleSelSelections();
			});
			
			
			/**
			 * 
			 * 功能说明:每次选择时重新获取表格中被选中对象
			 * 
			 * @author ZHONGFUHUA
			 * 
			 * @Date 2016年10月25日
			 * 
			 */
			$scope.$rolePrivSelectTable.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table', function () {
					$scope.getRolePrivSelSelections();
			});
			
				
			/**
			 * 
			 * 功能说明:获取表格中被选中的对象
			 * 
			 * @author ZHONGFUHUA
			 * 
			 * @Date 2016年10月25日
			 * 
			 */	
			$scope.getRolePrivSelections = function() {
				$scope.rolePrivSelections = [];
				$scope.rolePrivSelectionIds = [];
				return $.map($scope.$rolePrivTable.bootstrapTable('getSelections'), function (row) {
					$scope.rolePrivSelections.push(row);
					$scope.rolePrivSelectionIds.push(row.id);
					return row;
				});
			}
			
			
			/**
			 * 
			 * 功能说明:获取表格中被选中的对象
			 * 
			 * @author ZHONGFUHUA
			 * 
			 * @Date 2016年10月25日
			 * 
			 */
			$scope.getUserRoleSelections = function() {
				$scope.userRoleSelections = [];
				$scope.userRoleSelectionIds = [];
				return $.map($scope.$userRoleTable.bootstrapTable('getSelections'), function (row) {
					$scope.userRoleSelections.push(row);
					$scope.userRoleSelectionIds.push(row.id);
					return row;
				});
			}
			
			
			/**
			 * 
			 * 功能说明:获取表格中被选中的对象
			 * 
			 * @author ZHONGFUHUA
			 * 
			 * @Date 2016年10月25日
			 * 
			 */	
			$scope.getUserRoleSelSelections = function() {
					$scope.userRoleSelSelections = [];
					$scope.userRoleSelSelectionIds = [];
					return $.map($scope.$userRoleSelectTable.bootstrapTable('getSelections'), function (row) {
						$scope.userRoleSelSelections.push(row);
						$scope.userRoleSelSelectionIds.push(row.id);
						return row;
					});
			}
		
		
			/**
			 * 
			 * 功能说明:获取表格中被选中的对象
			 * 
			 * @author ZHONGFUHUA
			 * 
			 * @Date 2016年10月25日
			 * 
			 */
			$scope.getRolePrivSelSelections = function() {
				$scope.rolePrivSelSelections = [];
				$scope.rolePrivSelSelectionIds = [];
				return $.map($scope.$rolePrivSelectTable.bootstrapTable('getSelections'), function (row) {
						$scope.rolePrivSelSelections.push(row);
						$scope.rolePrivSelSelectionIds.push(row.id);
						return row;
				});
			}
		
		
		  
			/**
			 * 
			 * 功能说明:添加用户
			 * 
			 * @author ZHONGFUHUA
			 * 
			 * @Date 2016年10月25日
			 * 
			 */
			$scope.addUser = function(){
				for (var i = 0; i < $scope.userRoleSelSelections.length; i++) {
					var isAdd = true;
					for (var j = 0; j < $scope.selectedUsers.length; j++) {
						if ($scope.userRoleSelSelections[i] == $scope.selectedUsers[j]) {
							isAdd = false;
						}
					}
					if (isAdd) {
						$scope.selectedUsers.push($scope.userRoleSelSelections[i]);
					}
				}
				//$scope.$userRoleTable.bootstrapTable("selectPage", 1);
			};
			
	
			/**
			 * 
			 * 功能说明:删除用户
			 * 
			 * @author ZHONGFUHUA
			 * 
			 * @Date 2016年10月25日
			 * 
			 */
			$scope.deleteUser = function() {
				var ii = $scope.selectedUsers.length;
				for (var i = 0; i < ii; i++) {
					if ($scope.selectedUsers[i].xxx) {
						$scope.selectedUsers.splice(i, 1);
						i--;
					}
				}
			};
				
		 
				
			/**
			 * 
			 * 功能说明:新增权限
			 * 
			 * @author ZHONGFUHUA
			 * 
			 * @Date 2016年10月25日
			 * 
			 */
			$scope.addPrivilege = function() {
				for (var i = 0; i < $scope.rolePrivSelSelections.length; i++) {
					var isAdd = true;
					for (var j = 0; j < $scope.selectedPrivileges.length; j++) {
						if ($scope.rolePrivSelSelections[i] == $scope.selectedPrivileges[j]) {
							isAdd = false;
						}
					}
					if (isAdd) {
						$scope.selectedPrivileges.push($scope.rolePrivSelSelections[i]);
					}
				}
				//$scope.$rolePrivTable.bootstrapTable("selectPage", 1);
			};
				
			/**
			 * 
			 * 功能说明:删除权限
			 * 
			 * @author ZHONGFUHUA
			 * 
			 * @Date 2016年10月25日
			 * 
			 */
			$scope.deletePrivilege = function() {
				var ii = $scope.selectedPrivileges.length;
				for (var i = 0; i < ii; i++) {
					if ($scope.selectedPrivileges[i].xxx) {
						$scope.selectedPrivileges.splice(i, 1);
						i--;
					}
				}
			};
		
			/**
			 * 
			 * 功能说明:新增角色
			 * 
			 * @author ZHONGFUHUA
			 * 
			 * @Date 2016年10月25日
			 * 
			 */
			$scope.saveRole = function() {
				if ($scope.action == "ADD_ROLE") {
					$scope.submitRole.id = null;
				}
				
				for (var i = 0; i < $scope.selectedUsers.length; i++) {
					$scope.submitRole.userIdsList.push($scope.selectedUsers[i].id);
				}
				
				for (var i = 0; i < $scope.selectedPrivileges.length; i++) {
					$scope.submitRole.privilegeIdsList.push($scope.selectedPrivileges[i].id);
				}
				
				sysService.addRole($scope.submitRole, function(result) {
					if (!result.result) {
						$scope.error = result.msgTitle;
						$scope.isSuccess = false;
						return false;
					}
					new MessageDialog().success("保存成功");
					$scope.$roleEditModal.modal("hide");
					$scope.initRoleTree();
					$scope.$userRoleTable.bootstrapTable("selectPage", 1);
					$scope.$rolePrivTable.bootstrapTable("selectPage", 1);
					$scope.submitRole.roleName = null;
					$scope.selectedUsers = [];
					$scope.selectedPrivileges = [];
				}, function(error) {
					$scope.error = error;
					$scope.isSuccess = false;
				});
			};
						
			

			/**
			 * 
			 * 功能说明:删除角色
			 * 
			 * @author ZHONGFUHUA
			 * 
			 * @Date 2016年10月25日
			 * 
			 */
			$scope.deleteRole = function() {
				$scope.deleteRoleParam.id = $scope.roleId;
				new MessageDialog().deletion("确定删除?", function() {
					sysService.deleteRole($scope.deleteRoleParam,
						function(result) {
							if (!result.result) {
								$scope.error = result.msgTitle;
								$scope.isSuccess = false;
								return false;
							}
							$scope.initRoleTree();
							$scope.$userRoleTable.bootstrapTable("selectPage",1);
							$scope.$rolePrivTable.bootstrapTable("selectPage",1);
						},function(error) {
							$scope.error = error;
							$scope.isSuccess = false;
						}
					);
				});
			};
						
						
						
			/**
			 * 
			 * 功能说明:根据角色删除用户
			 * 
			 * @author ZHONGFUHUA
			 * 
			 * @Date 2016年10月25日
			 * 
			 */
			$scope.deleteUser = function(){
				$scope.deleteUserParams.userIdsList = $scope.userRoleSelectionIds;
				new MessageDialog().deletion("确定删除？", function() {
					sysService.deleteUserByRoldId($scope.deleteUserParams,
						function(result) {
							if (!result.result) {
								$scope.error = result.msgTitle;
								$scope.isSuccess = false;
								return false;
							}
							$scope.$userRoleTable.bootstrapTable("selectPage", 1);
							$scope.$removeUserButton.prop('disabled', true);
						},function(error) {
							$scope.error = error;
							$scope.isSuccess = false;
						}
					);
				});
			};
	
						
			/**
			 * 
			 * 功能说明:根据角色删权限
			 * 
			 * @author ZHONGFUHUA
			 * 
			 * @Date 2016年10月25日
			 * 
			 */
			$scope.deletePriv = function(){
				$scope.deletePrivParams.privilegeIdsList = $scope.rolePrivSelectionIds;
				new MessageDialog().deletion("确定删除？", function() {
					sysService.deletePrivilegeByRoldId($scope.deletePrivParams,
						function(result) {
							if (!result.result) {
								$scope.error = result.msgTitle;
								$scope.isSuccess = false;
								return false;
							}
							$scope.$rolePrivTable.bootstrapTable("selectPage", 1);
							$scope.$removePrivButton.prop('disabled', true);
						},function(error) {
							$scope.error = error;
							$scope.isSuccess = false;
						}
					);
				});
			};
						
						
			/**
			 * 
			 * 功能说明:弹出框关闭事件
			 * 
			 * @author ZHONGFUHUA
			 * 
			 * @Date 2016年10月25日
			 * 
			 */
			$scope.$roleEditModal.on("hidden.bs.modal",function(){
				$scope.submitRole.roleName = null;
				$scope.submitRole.userIdsList = [];
				$scope.submitRole.privilegeIdsList = [];
				
				$scope.queryUserParams.orgId = null;
				$scope.queryPrivilegeParams.privCatalogId = null;
				
				$scope.rolePrivSelSelections = [];
				$scope.userRoleSelSelections = [];
				$scope.selectedPrivileges = [];
				$scope.selectedUsers = [];
			});
			
			/**
			 * 
			 * 功能说明:弹出框打开事件
			 * 
			 * @author ZHONGFUHUA
			 * 
			 * @Date 2016年10月25日
			 * 
			 */
			$scope.$roleEditModal.on('shown.bs.modal',function(){
				$scope.initOrgTree();
			 	$scope.initPrivTree();
				$scope.$userRoleSelectTable.bootstrapTable("selectPage", 1);
				$scope.$rolePrivSelectTable.bootstrapTable("selectPage", 1);
			});
						
						
			$(function() {
				
				 	$scope.initRoleTree();
				 	
					$scope.$userRoleTable.bootstrapTable({
						queryParams: function(params) {
							var selfParams = {
									"children": []
							};
							if ($scope.roleId != null) {
								selfParams.children.push({"name":"systemRoleId", "value":$scope.roleId});
							}
							return $.extend({}, params, selfParams);
						},
						ajax : function(render) {
							if (!$scope.roleId) {
								ffc.util.loadTableData(render);
							} else {
								sysService.queryUserDataByRoleId(render.data, 
									function(result) {
										if(!result.result){
											$scope.error = result.msgTitle;
											$scope.isSuccess = false;
											return;
										}
										ffc.util.loadTableData(render, result);
									},function(error){
										$scope.error = error;
										$scope.isSuccess = false;
									}
								);
							}
						}
					});
					
					$scope.$rolePrivTable.bootstrapTable({
						queryParams: function(params) {
							var selfParams = {
								"children": []
							};
							if ($scope.roleId!=null) {
								selfParams.children.push({"name":"systemRoleId", "value":$scope.roleId});
							}
							return $.extend({}, params, selfParams);
						},
						ajax : function(render) {
							if (!$scope.roleId) {
								ffc.util.loadTableData(render);
							} else {
								sysService.queryPrivilegeDataByRoleId(render.data, 
									function(result) {
										if(!result.result){
											$scope.error = result.msgTitle;
											$scope.isSuccess = false;
											return;
										}
										ffc.util.loadTableData(render, result);
									},function(error){
										$scope.error = error;
										$scope.isSuccess = false;
									}
								);
							}
						}
					});
					
					$scope.$userRoleSelectTable.bootstrapTable({
						queryParams: function(params) {
							var selfParams = {
									"children": []
							};
							if ($scope.queryUserParams.orgId != null) {
								selfParams.children.push({"name":"orgId", "value":$scope.queryUserParams.orgId});
							}
							return $.extend({}, params, selfParams);
						},
						ajax : function(render) {
							if (!$scope.queryUserParams.orgId) {
								ffc.util.loadTableData(render);
							} else {
								sysService.queryUserDataByMap(render.data, 
									function(result) {
										if(!result.result){
											$scope.error = result.msgTitle;
											$scope.isSuccess = false;
											return;
										}
										ffc.util.loadTableData(render, result);
									},function(error){
										$scope.error = error;
										$scope.isSuccess = false;
									}
								);
							}
						}
					});
					
					$scope.$rolePrivSelectTable.bootstrapTable({
						queryParams: function(params) {
							var selfParams = {
									"children": []
							};
							if ($scope.queryPrivilegeParams.privCatalogId != null) {
								selfParams.children.push({"name":"privCatalogId", "value":$scope.queryPrivilegeParams.privCatalogId});
							}
							return $.extend({}, params, selfParams);
						},
						ajax : function(render) {
							if (!$scope.queryPrivilegeParams.privCatalogId) {
								ffc.util.loadTableData(render);
							} else {
								sysService.queryPrivDataByMap(render.data, 
									function(result) {
										if(!result.result){
											$scope.error = result.msgTitle;
											$scope.isSuccess = false;
											return;
										}
										ffc.util.loadTableData(render, result);
									},function(error){
										$scope.error = error;
										$scope.isSuccess = false;
									}
								);
							}
						}
					});
			});
}])